package com.goldenautumn.prefabricate.activity.mine.order

import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.cart.SettlementGoodsAdapter
import com.goldenautumn.prefabricate.activity.mine.adapter.AfterSalesAdapter
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.order.OrderStatusUiState
import com.goldenautumn.prefabricate.business.order.OrderViewModel
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.databinding.ActivityApplyRefundDetailsBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.compat.gone

class ApplyRefundDetailsActivity : BaseActivity<ActivityApplyRefundDetailsBinding>() {

    private val mOrderModel by viewModels<OrderViewModel>()

    private val orderID: Int?
        get() = intent?.getIntExtra("ORDER",0)

    override fun getLayoutId(): Int = R.layout.activity_apply_refund_details

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)

        var mAdapter = SettlementGoodsAdapter()

        mBinding.goodsRv.layoutManager = LinearLayoutManager(this)
        mBinding.goodsRv.adapter = mAdapter


        mOrderModel.getRefundDetails("$orderID")


        while (isActive) {
            select {
                mOrderModel.orderStatusUiState.onReceive {
                    when (it) {
                        is OrderStatusUiState.RefundDetails -> {
                            mBinding.status.text = it.refund?.is_agree?.text
                            mBinding.msg.text = if(it.refund?.is_agree?.value == "0") getString(R.string.customer_service_helping) else getString(R.string.rrocessed_after_sales)
                            mBinding.totalPrice1.text = String.format(getString(R.string.currency),"${it.refund?.order_goods?.goods_price}")
                            mBinding.totalPrice2.text = String.format(getString(R.string.currency),"${it.refund?.order_goods?.goods_price}")
                            mBinding.time.text = it.refund?.create_time
                            mBinding.orderSn.text = "${it.refund?.order_refund_id}"
                            if(it.refund?.is_agree?.value != "0"){
                                mBinding.view5.setBackgroundColor(ContextCompat.getColor(this@ApplyRefundDetailsActivity,R.color.colorPrimary))
                                mBinding.resultTv.setTextColor(ContextCompat.getColor(this@ApplyRefundDetailsActivity,R.color.colorPrimary))
                                mBinding.resultTv.setCompoundDrawablesWithIntrinsicBounds(null,ContextCompat.getDrawable(this@ApplyRefundDetailsActivity,R.mipmap.ic_sales_dui),null,null)
                            }
                            var goods = ArrayList<GoodsInfoVO>()
                            goods.add(
                                GoodsInfoVO(
                                    goods_image = it.refund?.order_goods?.image?.file_path,
                                    goods_name = it.refund?.order_goods?.goods_name,
                                    goods_sku = GoodsSkuVo(
                                        goods_id = it.refund?.order_goods?.goods_id,
                                        goods_attr = it.refund?.order_goods?.goods_attr,
                                        goods_price = it.refund?.order_goods?.goods_price,
                                        spec_sku_id = "${it.refund?.order_refund_id}"
                                    ),
                                    total_num = it.refund?.order_goods?.total_num
                                )
                            )
                            mAdapter.submitList(goods)


                        }

                        else -> {}
                    }
                }
            }
        }
    }

}